<?php if (!isset($_SESSION)) session_start(); ?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">

<title>Koreanet</title>

<!-- Nanum Webfont -->
<style>
* {
	font-family: 'NanumGothic' !important;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
 google.load( "webfont", "1" );
 google.setOnLoadCallback(function() {
  WebFont.load({ custom: {
   families: [ "NanumGothic" ],
   urls: [ "http://fontface.kr/NanumGothic/css" ]
  }});
 });
</script>

<!-- Bootstrap core CSS -->
<link href="./dist/css/bootstrap.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="./css/daily_report.css" rel="stylesheet">
<link rel="stylesheet"
	href="./lib/datetimepicker/bootstrap-datetimepicker.min.css" />
</head>

<body>
<?php include 'header.php';?>
<?php require_once './class/DailyReport.inc';?>
<?php require_once './class/Category.inc';?>
<?php require_once './class/Attendance.inc';?>
<?php

$getType = $_GET ['type'];
$getId = $_GET ['id'];
$date = $_GET ['date'];
$dReport = new DailyReport ();
$dReport->search4Detail ( $date, $getId );

if ($getType == 1) {
	if ($login == null || $login->getGrade () > 2) {
		echo "<script type='text/javascript'>";
		echo "location.replace('./main.php');";
		echo "</script>";
		exit ();
	}
}
?>
	<div class="container container_bottom">
		<div class="row row_margin">
			<div class="col-md-12">
				<h2>일일 결산</h2>
			</div>
		</div>
		<form class="form-signin" role="form"
			<?php
			if ($getType == null || empty ( $getType )) {
				echo "action='./ajax/modify_daily_report.php'";
			} else if ($getType == 1) {
				echo "action='./ajax/create_daily_report.php'";
			}
			?>
			method="post">
			<div class="row row_margin">
				<div class="col-md-6">
					<div class="row row_margin row_top">
						<div class="input-group date" id="picker1">
							<span class="input-group-addon">날짜</span> <input type="text"
								required="required" name="search_dt" id="search_dt"
								class="form-control" placeholder="달력 버튼을 누르세요."
								data-format="YYYY/MM/DD" readonly="readonly"
								<?php
								if ($getType == 1) {
									echo "value='" . date ( 'Y/m/d', strtotime ( '-1 day' ) ) . "'";
								} else {
									echo "value='" . $dReport->search_dt . "'";
								}
								?> />
								<?php
								if ($getType == 1) {
									echo "<span class='input-group-addon input-group-addon-end add-last'>";
									echo "<button class='btn btn-default btn-calendar'";
									echo ">달력</button></span>";
								}
								?>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin row_top">
						<div class="input-group">
							<span class="input-group-addon">사원번호</span> <input type="text"
								required="required" id="mem_id" name="mem_id"
								class="form-control" placeholder="사원번호 없음"
								<?php if ($getType == null || empty($getType)) echo "readonly='readonly'"; ?>
								<?php
								echo "value='" . $dReport->mem_id . "'";
								echo " maxlength='6'";
								echo " onkeyup='javascript:checkNumber(this,false)'";
								?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">성명</span> <input type="text"
								name="mem_nm" class="form-control" placeholder="성명 없음"
								readonly="readonly" <?php echo "value='".$dReport->mem_nm."'"?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">직책</span> <input type="text"
								name="positions" class="form-control" placeholder="직책 없음"
								readonly="readonly"
								<?php echo "value='".$dReport->positions."'"?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">차량식별번호</span> <input type="text"
								name="car_id" class="form-control" placeholder="차량식별번호 없음"
								readonly="readonly" <?php echo "value='".$dReport->car_id."'"?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">자동차등록번호판</span> <input
								type="text" name="license_plate" class="form-control"
								placeholder="자동차등록번호판 없음" readonly="readonly"
								<?php echo "value='".$dReport->license_plate."'"?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">거래처명</span> <input type="text"
								name="cusm_cd" class="form-control" placeholder="거래처명 없음"
								readonly="readonly" <?php echo "value='".$dReport->cusm_cd."'"?>>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">근무점포</span> <input type="text"
								name="brno" class="form-control" placeholder="근무점포 없음"
								readonly="readonly" <?php echo "value='".$dReport->brno."'"?>>
						</div>
					</div>
				</div>
				<!-- 추가 부분 -->
				<div class="col-md-6">
					<div class="row row_margin">
						<div class="input-group">
							<span class="input-group-addon">기사구분</span> <input type="text"
								name="brno" class="form-control" placeholder="기사구분 없음"
								readonly="readonly"
								<?php echo "value='".$dReport->car_soyu_nm."'"?>>
						</div>
					</div>
				</div>
				<!-- 추가 부분 -->
			</div>
			<div class="table-responsive col-md-12 row_margin" style="overflow: scroll;">
				<table class="table table-bordered" id="attendance_table">
			 		<?php
						$atten = new Attendance ();
						$atten->searchAttendance ( $dReport->mem_id, $dReport->search_dt );
						?>
				</table>
			</div>
			<div class="row row_margin">
				<div class="col-md-12">
					<form name="form" method="post">
						<div class="row row_margin row_top">
							<div class="input-group">
								<span class="input-group-addon">근태</span>
								<div class="form-control">
								<?php
								// 추가 부분
								if ($dReport->car_soyu_tp != '7') {
									echo "출근";
									echo "<input type='radio' name='attend_tp' value=Y ";
									if (strnatcasecmp ( $dReport->attend_tp, "Y" ) == 0 || $getType == 1) {
										echo "checked";
									}
									echo " >";
									// echo "onclick='chkCondition(this)'>";
									echo "&nbsp; &nbsp;반차";
									echo "<input type='radio' name='attend_tp' value=H ";
									if (strnatcasecmp ( $dReport->attend_tp, "H" ) == 0) {
										echo "checked";
									}
									echo " >";
									echo "&nbsp; &nbsp;휴무";
									echo "<input type='radio' name='attend_tp' value=N ";
									if (strnatcasecmp ( $dReport->attend_tp, "N" ) == 0) {
										echo "checked";
									}
									echo " >";
								} else {
									// 4대보험일 경우
									echo "<input type='text' id='work_time' name='work_time' class='form-control' placeholder='ex> 8.0  (시간을 소수점 포함 숫자로 입력하세요.)'";
									echo "value='" . $dReport->attend_tp . "'";
									echo " maxlength='20'";
									echo " onkeyup='javascript:checkNumber(this,true)'>";
									echo "<span class='input-group-addon'>시간</span>";
								}
								?>
								</div>
							</div>
						</div>
						<!-- 						<div class="row row_margin"> -->
						<!-- 							<div class="input-group"> -->
						<!-- 								<span class="input-group-addon">차량</span> <select id='car_id' -->
						<!-- 									name='car_id' class='form-control'>  -->
							<?php
							// $category = new Category ();
							// $category->search4table ( '9', $dReport->subs_firm );
							//							?>
<!-- 							</select> -->
						<!-- 							</div> -->
						<!-- 						</div> -->
						<!-- 						<div class="row row_margin"> -->
						<!-- 							<div class="input-group"> -->
						<!-- 								<span class="input-group-addon">거래처명</span> <select -->
						<!-- 									id='car_id2222' name='car_id2222' class='form-control'>  -->
							<?php
							// $category = new Category ();
							// $category->search4table ( '9', $dReport->subs_firm );
							//							?>
<!-- 							</select> -->
						<!-- 							</div> -->
						<!-- 						</div> -->
						<!-- 						<div class="row row_margin"> -->
						<!-- 							<div class="input-group"> -->
						<!-- 								<span class="input-group-addon">근무점포</span> <select -->
						<!-- 									id='car_id3333' name='car_id3333' class='form-control'>  -->
							<?php
							// $category = new Category ();
							// $category->search4table ( '9', $dReport->subs_firm );
							//							?>
<!-- 							</select> -->
						<!-- 							</div> -->
						<!-- 						</div> -->
						<!-- 추가 부분 -->
						<div class='row row_margin' id='conditionCost1'>
							<div class="input-group">
								<span class="input-group-addon">휴무공제</span> <input type="text"
									id="close_deduct" name="close_deduct" class="form-control"
									placeholder="ex> 80000  (가격만 숫자로 입력하세요.)"
									<?php
									echo "value='" . $dReport->close_deduct . "'";
									echo " maxlength='20'";
									echo " onkeyup='javascript:checkNumber(this,true)'";
									?>> <span class="input-group-addon">원</span>
							</div>
						</div>
						<!-- 추가 부분 -->
						<div class="row row_margin">
							<div class="input-group">
								<span class="input-group-addon">용차업체</span> <select
									id='subs_firm_cd' name='subs_firm' class='form-control'
									onchange='chkCondition(this)'> 
							<?php
							$category = new Category ();
							$category->search4table ( '9', $dReport->subs_firm );
							?>
							</select>
							</div>
						</div>
						<div class='row row_margin' id='conditionCost2'>
							<div class="input-group">
								<span class="input-group-addon">용차비용</span> <input type="text"
									id="subs_amt" name="subs_amt" class="form-control"
									placeholder="ex> 80000  (가격만 숫자로 입력하세요.)"
									<?php
									echo "value='" . $dReport->subs_amt . "'";
									echo " maxlength='20'";
									echo " onkeyup='javascript:checkNumber(this,true)'";
									if ($getType == 1) {
										echo "disabled";
									} else {
										if ($dReport->subs_firm == '000000') {
											echo "disabled";
										}
									}
									?>> <span class="input-group-addon">원</span>
							</div>
						</div>
						<!-- TODO 개발 완료시까지 주석처리 -->
						<!-- 						<div class="row row_margin"> -->
						<!-- 							<div class="input-group"> -->
						<!-- 								<span class="input-group-addon">휴무공제액</span> <input type="text" -->
						<!-- 									id="day_off_cost" name="day_off_cost" class="form-control" -->
						<!-- 									placeholder="ex> 20000 (휴무로 인한 운송료 공제 비용을 숫자로만 입력하세요.)" -->
									<?php
									// echo "value='" . $dReport->day_off_cost . "'";
									// echo " maxlength='20'";
									// echo " onkeyup='javascript:checkNumber(this,true)'";
									?>
<!-- 									<span class="input-group-addon">원</span> -->
						<!-- 							</div> -->
						<!-- 						</div> -->
						<div class="row row_margin">
							<div class="input-group">
								<span class="input-group-addon">기타(지급)</span> <input type="text"
									id="etc_amt_1" name="etc_amt_1" class="form-control"
									placeholder="ex> 20000 (추가지급 비용을 숫자로만 입력하세요.)"
									<?php
									echo "value='" . $dReport->etc_amt_1 . "'";
									echo " maxlength='20'";
									echo " onkeyup='javascript:checkNumber(this,true)'";
									?>> <span class="input-group-addon">원</span>
							</div>
						</div>
						<div class="row row_margin">
							<div class="input-group">
								<span class="input-group-addon">기타(공제)</span> <input type="text"
									id="etc_amt_2" name="etc_amt_2" class="form-control"
									placeholder="ex> 10000 (추가공제 비용을 숫자로만 입력하세요.)"
									<?php
									echo "value='" . $dReport->etc_amt_2 . "'";
									echo " maxlength='20'";
									echo " onkeyup='javascript:checkNumber(this,true)'";
									?>> <span class="input-group-addon">원</span>
							</div>
						</div>
						<div class="row row_margin">
							<div class="input-group">
								<span class="input-group-addon">비고</span> <input type="text"
									id="note" name="note" class="form-control"
									placeholder="ex> 기타1 - 추가 통신비 지급, 기타2 - 기장료 2개월치 선납 (기타사항을 적으세요.)"
									<?php echo "value='".$dReport->note."'"?>>
							</div>
						</div>
				
				</div>

			</div>

			<div class="row row_margin">
				<div class="col-md-12 text-right">
					<button class="btn btn-lg btn-default" type="button"
						onclick="javascript:initData()">입력 초기화</button>
		<?php
		if ($getType == null || empty ( $getType )) {
			echo "<button class='btn btn-lg btn-success' type='submit'>결산완료</button>";
			echo "&nbsp;<button class='btn btn-lg btn-default' onclick='javascript:months_chk()' type='button'>월결생성</button>";
		} else if ($getType == 1) {
			echo "<button class='btn btn-lg btn-success' type='submit'>생성</button>";
		}
		?>
			<button class="btn btn-lg btn-danger" type="button"
						onclick="window.history.back();">취소</button>
				</div>
			</div>
		</form>
	</div>
	<!-- /container -->

	<!-- Bootstrap core JavaScript
    ================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->
	<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
	<script src="./dist/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./lib/moment/moment.js"></script>
	<script type="text/javascript" src="./lib/moment/transition.js"></script>
	<script type="text/javascript" src="./lib/moment/collapse.js"></script>
	<script type="text/javascript"
		src="./lib/datetimepicker/bootstrap-datetimepicker.min.js"></script>

	<script type="text/javascript">
	$(window).load(function () {


		$('#picker1').datetimepicker({
			pickTime: false
		});
		checkNumber(document.getElementById("subs_amt"), true);
		checkNumber(document.getElementById("etc_amt_1"), true);
		checkNumber(document.getElementById("etc_amt_2"), true);
		checkNumber(document.getElementById("close_deduct"), true);
		chkCondition(document.getElementById("subs_firm_cd"));
	});

	$(document).ready(function () {
		$('#mem_id').tooltip({'trigger':'focus', 'title': '최대 6자 이내 숫자만 입력하세요.'});
		$('#subs_amt').tooltip({'trigger':'focus', 'title': '최대 20자 이내 숫자만 입력하세요.'});
		$('#etc_amt_1').tooltip({'trigger':'focus', 'title': '최대 20자 이내 숫자만 입력하세요.'});
		$('#etc_amt_2').tooltip({'trigger':'focus', 'title': '최대 20자 이내 숫자만 입력하세요.'});
		$('#note').tooltip({'trigger':'focus', 'title': '최대 500자 이내로 자유롭게 입력하세요.'});
	});
	
	function chkCondition(list) {
// 		var val = radio.value;
// 		var cost = document.getElementById("conditionCost");
// 		if (val == 'Y') {
// 			document.getElementById("subs_amt").value = 0;
// 			cost.style.display = 'none';
// 		} else if (val == 'N'){
// 			cost.style.display = 'inline';
// 		}
		var val = list.value;
		if (val != "000000") {
			document.getElementById("subs_amt").disabled = false;
		} else {
			document.getElementById("subs_amt").disabled = true;
		}
	}
	function checkNumber(obj, isWon)
	{
		if (obj.value.length > 0)
		{
			for (var i = 0; i < obj.value.length; i++)
			{
				if (!(isWon && obj.value.charAt(i) == ',') && (obj.value.charAt(i) < '0' || obj.value.charAt(i) > '9')) {
					obj.value = obj.value.substr(0,i);
				}
			}
			if (isWon) {
				obj.value = commaNum(obj.value);
			}
		}
	}

	function commaNum(num) {
		num = num.replace(/,/gi,'');
		var len, point, str;
		num = num + "";
		point = num.length % 3;
		len = num.length;

		str = num.substring(0, point);
		while (point < len) {
			if (str != "") str += ",";
			str += num.substring(point, point + 3);
			point += 3;
		}
		return str;
	}

	function initData() {
		document.getElementById("search_dt").value = "";
		document.getElementById("mem_id").value = "";
		document.getElementById("subs_amt").value = "";
		document.getElementById("day_off_cost").value = "";
		document.getElementById("etc_amt_1").value = "";
		document.getElementById("etc_amt_2").value = "";
		document.getElementById("note").value = "";
	}
	function months_chk(){
		var sendData = 'type=1&search_dt=' + encodeURIComponent('<?php echo $dReport->search_dt; ?>') + '&mem_id=' + encodeURIComponent('<?php echo $dReport->mem_id; ?>');

		$.post(
			"ajax/monthly_report.php",
			sendData,
			function (msg) {

				var spos = msg.indexOf("<div>")+5;
				var epos = msg.indexOf("</div>");
				msg = msg.substr(spos,epos-spos);

				if(msg == "Y"){
					alert("월결이 이미 생성되었습니다.");
				}else{
					if(confirm("["+msg+"]월결을 생성하시겠습니까?") ){
						months_create(msg)
					}
				}
		});
	}
	function months_create(sply_dt){
		var sendData = 'type=0&search_dt=' + encodeURIComponent(sply_dt) + '&mem_id=' + encodeURIComponent('<?php echo $dReport->mem_id; ?>');

		$.post(
			"ajax/monthly_report.php",
			sendData,
			function (msg) {
				if(confirm("월결 상세페이지로 이동하시겠습니까?") ){
					//alert('./../monthly_report.php?id=<?php echo $dReport->mem_id; ?>&time='+sply_dt);
					location.replace('./../monthly_report.php?id=<?php echo $dReport->mem_id; ?>&time='+sply_dt);
				}
		});
	}
	</script>
</body>
</html>